﻿using System.Windows.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Keyboard = Microsoft.Xna.Framework.Input.Keyboard;

namespace Silverlight3dApp.Learning.Lesson05
{
    public class CollisionGame : Game
    {
        private SpriteBatch _spriteBatch;

        private Texture2D _backgroundTexture;
        private SoundEffect _soundEffect;
        private SpriteFont _scorefont;

        private SpriteManager _spriteManager;
        private int _currentScore;

        private GameState _currentGameState = GameState.Start;

        public GameState CurrentGameState
        {
            get
            {
                return _currentGameState;
            }
            set
            {
                _currentGameState = value;
            }
        }

        public CollisionGame()
        {
            Content.RootDirectory = "Content";
        }

        public void AddScore(int score)
        {
            _currentScore += score;
        }

        protected override void Initialize()
        {
            _spriteManager = new SpriteManager(this);
            Components.Add(_spriteManager);
            _spriteManager.Enabled = false;
            _spriteManager.Visible = false;

            base.Initialize();
        }

        protected override void LoadContent()
        {
            _spriteBatch = new SpriteBatch(GraphicsDevice);

            _backgroundTexture = Content.Load<Texture2D>(@"Images/background");

            _soundEffect = Content.Load<SoundEffect>(@"Audios/start");
            _soundEffect.Play();

            _scorefont = Content.Load<SpriteFont>(@"Fonts/score");

            base.LoadContent();
        }

        protected override void Update(GameTime gameTime)
        {
            switch (_currentGameState)
            {
                case GameState.Start:
                    if (Keyboard.GetState().GetPressedKeys().Length > 0)
                    {
                        _currentGameState = GameState.InGame;
                        _spriteManager.Enabled = true;
                        _spriteManager.Visible = true;
                    }
                    break;
                case GameState.InGame:
                    break;
                case GameState.GameOver:
                    _spriteManager.Enabled = false;
                    _spriteManager.Visible = false;
                    if (Keyboard.GetState().IsKeyDown(Key.Enter))
                    {
                        Exit();
                    }
                    break;
            }
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            switch (_currentGameState)
            {
                case GameState.Start:
                    GraphicsDevice.Clear(Colors.AliceBlue);
                    _spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend);
                    var startText = "Avoid the blades or die!";
                    _spriteBatch.DrawString(_scorefont, startText, new Vector2(GraphicsDevice.Viewport.Width / 2.0f - _scorefont.MeasureString(startText).X / 2.0f, GraphicsDevice.Viewport.Height / 2.0f - _scorefont.MeasureString(startText).Y / 2.0f), Colors.SaddleBrown, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
                    startText = "(Press any key to begin)";
                    _spriteBatch.DrawString(_scorefont, startText, new Vector2(GraphicsDevice.Viewport.Width / 2.0f - _scorefont.MeasureString(startText).X / 2.0f, (GraphicsDevice.Viewport.Height / 2.0f - _scorefont.MeasureString(startText).Y / 2.0f) + 30), Colors.SaddleBrown, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
                    _spriteBatch.End();
                    break;
                case GameState.InGame:
                    GraphicsDevice.Clear(Colors.CornflowerBlue);
                    _spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend);
                    _spriteBatch.Draw(_backgroundTexture, GraphicsDevice.Viewport.Bounds, null, Colors.White, 0, Vector2.Zero, SpriteEffects.None, 0);
                    _spriteBatch.DrawString(_scorefont, string.Format("Score: {0}", _currentScore), new Vector2(10, 10), Colors.Brown, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
                    _spriteBatch.End();
                    break;
                case GameState.GameOver:
                    GraphicsDevice.Clear(Colors.AliceBlue);
                    _spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend);
                    var gameOverText = "Game over! The blades win again!";
                    _spriteBatch.DrawString(_scorefont, gameOverText, new Vector2(GraphicsDevice.Viewport.Width / 2.0f - _scorefont.MeasureString(gameOverText).X / 2.0f, GraphicsDevice.Viewport.Height / 2.0f - _scorefont.MeasureString(gameOverText).Y / 2.0f), Colors.SaddleBrown, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
                    gameOverText = string.Format("Your score: {0}", _currentScore);
                    _spriteBatch.DrawString(_scorefont, gameOverText, new Vector2(GraphicsDevice.Viewport.Width / 2.0f - _scorefont.MeasureString(gameOverText).X / 2.0f, (GraphicsDevice.Viewport.Height / 2.0f - _scorefont.MeasureString(gameOverText).Y / 2.0f) + 30), Colors.SaddleBrown, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
                    gameOverText = "(Press ENTER to exit)";
                    _spriteBatch.DrawString(_scorefont, gameOverText, new Vector2(GraphicsDevice.Viewport.Width / 2.0f - _scorefont.MeasureString(gameOverText).X / 2.0f, (GraphicsDevice.Viewport.Height / 2.0f - _scorefont.MeasureString(gameOverText).Y / 2.0f) + 60), Colors.SaddleBrown, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
                    _spriteBatch.End();
                    break;
            }
            base.Draw(gameTime);
        }
    }
}